package com.cljh.controller;

import com.cljh.bean.Sale;
import com.cljh.bean.SaleTable;
import com.cljh.controller.page.*;
import com.cljh.service.SaleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sales")
public class SaleController {
    @Autowired
    SaleService saleService;

    @PostMapping("/{pn}/{size}")
    public  Result  selectAllByCondition(@PathVariable(value = "pn") Integer pn, @PathVariable(value = "size")Integer size, @RequestBody SaleTable saleTable){
        PageBean<SaleTable> saleTablePageBean = saleService.selectAllByCondition(pn, size, saleTable);
        Integer code=saleTablePageBean !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=saleTablePageBean!=null ? "查询到信息":"未查询到信息";
        return new Result(code,saleTablePageBean,msg);
    }

    @DeleteMapping("/{id}")
    public  Result  deleteId(@PathVariable Integer id){
        boolean delete = saleService.deleteId(id);
        flushRedis(Code.SALE_ALL);
        flushRedis(Code.SALES_CHILD);
        flushRedis(Code.SALES_MAN);
        flushRedis(Code.SALES_WOMAN);
        flushRedis(Code.SALES_ALL);
        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }

    @PutMapping
    public  Result  upd(@RequestBody SaleTable saleTable){

        boolean update = saleService.upd(saleTable);
        flushRedis(Code.SALE_ALL);
        flushRedis(Code.SALES_CHILD);
        flushRedis(Code.SALES_MAN);
        flushRedis(Code.SALES_WOMAN);
        flushRedis(Code.SALES_ALL);
        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }
    @DeleteMapping("/deleteIds")
    public  Result  deleteAll(@Param("ids") List<Integer> ids){
        boolean b = saleService.deleteAll(ids);
        flushRedis(Code.SALE_ALL);
        flushRedis(Code.SALES_CHILD);
        flushRedis(Code.SALES_MAN);
        flushRedis(Code.SALES_WOMAN);
        flushRedis(Code.SALES_ALL);
        Integer code=b ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=b ? "删除成功":"删除失败";
        return new Result(code,b,msg);
    }


    @GetMapping("/{id}")
    public  Result  selectBySaleId(@PathVariable Integer id){
        SaleTable saleTable = saleService.selectBySaleId(id);
        Integer code=saleTable !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=saleTable!=null ? "查询到信息":"未查询到信息";
        return new Result(code,saleTable,msg);
    }


    @GetMapping("/user/{username}")
    public  Result  selectUser(@PathVariable String  username){
        System.out.println(username);
        List<SaleTable> saleTable = saleService.selectByUsername(username);
        Integer code=saleTable !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=saleTable!=null ? "查询到信息":"未查询到信息";
        return new Result(code,saleTable,msg);
    }




    @PostMapping("/save")
    public Result save( @RequestParam("addId") Integer addId,
                        @RequestParam("payCount")Double payCount,
                        @RequestParam("sumCount") Long sumCount,
                        @RequestParam("username") String username,
                        @RequestParam("userId") Integer userId,
                        @RequestBody List<ProductPageBean> multi
                        ){
        SaleTable saleTable = new SaleTable();
        if (addId!=null){
            saleTable.setAddId(addId);
        }
        if (payCount!=null){
            saleTable.setPayCount(payCount);
        }
        if (sumCount!=null){
            saleTable.setSumCount(sumCount);
        }
        if (username!=null){
            saleTable.setUsername(username);
        }
        System.out.println(multi);
        System.out.println(saleTable);
        boolean b = saleService.saveSaleTable(saleTable,multi,userId);
//        boolean b=true;
        flushRedis(Code.SALE_ALL);
        flushRedis(Code.SALES_CHILD);
        flushRedis(Code.SALES_MAN);
        flushRedis(Code.SALES_WOMAN);
        flushRedis(Code.SALES_ALL);

        Integer code=b ? Code.SAVE_OK:Code.SAVE_ERR;
        String msg=b ? "添加成功":"添加失败";
        return new Result(code,b,msg);
    }
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    //删除缓存
    public void flushRedis(String key){
        String s = stringRedisTemplate.opsForValue().get(key);
        if (s!=null&&!"".equals(s)){
            stringRedisTemplate.delete(key);
        }
    }
}
